package org.mnj07.jms.queue;

import javax.annotation.Resource;
import javax.jms.*;
import java.util.Date;

public class SenderDI
{
  @Resource(lookup = "jms/javaee6/ConnectionFactory")
  private static ConnectionFactory connectionFactory;
  @Resource(lookup = "jms/javaee6/Queue")
  private static Queue queue;

  public static void main(String[] args) {

      try {
          // Creates the needed artifacts to connect to the queue
          Connection connection = connectionFactory.createConnection();
          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
          MessageProducer producer = session.createProducer(queue);

          // Sends a text message to the queue
          TextMessage message = session.createTextMessage();
          message.setText("This is a text message sent at " + new Date());
          producer.send(message);
          System.out.println("\nMessage sent !");

          connection.close();

      } catch (Exception e) {
          e.printStackTrace();
      }

      System.exit(0);
  }
}
